package com.gem.talkbase.activity;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import net.tsz.afinal.FinalBitmap;

import com.gem.talkbase.R;
import com.gem.talkbase.client.Client;
import com.gem.talkbase.util.StreamTools;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class Fragment_find_AddFriend extends Activity{

	private String who,who_zan,u_nick;
	private TextView username,nicheng,sex,add_friend,message_friend,zan;
	private ImageView imageview_my,imageview_zan,imageview_top1,imageview_top2,imageview_top3;
	private FinalBitmap fb;
	int fuzhu1,fuzhu2,fuzhu3;

	private final static  String URL_ZAN = Client.URL_ZAN;
	private Handler handler = new Handler(){

		@Override

		public void handleMessage(Message msg) {

			switch(msg.what){

			case 1: 
				//Toast.makeText(Fragment_find_AddFriend.this, "赞成功", Toast.LENGTH_SHORT).show();
				break;
			case 2: 
				//Toast.makeText(Fragment_find_AddFriend.this, "赞失败", Toast.LENGTH_SHORT).show();

				break;
			}
		}
	};

	@Override  
	protected void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);  
		// 去标题栏  
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
		setContentView(R.layout.fragment_find_addfriend);

		username= (TextView) findViewById(R.id.username);
		nicheng= (TextView) findViewById(R.id.nicheng);
		sex= (TextView) findViewById(R.id.sex);

		add_friend = (TextView) findViewById(R.id.addfriend);//监听加好友
//		message_friend = (TextView) findViewById(R.id.messagefriend);//监听发消息
		zan = (TextView) findViewById(R.id.zan);//显示赞

		fb = FinalBitmap.create(this);//初始化FinalBitmap模块
		imageview_my = (ImageView) findViewById(R.id.imageview);//显示我的头像
		imageview_zan = (ImageView) findViewById(R.id.imageview_zan);//赞图片
		imageview_top1 = (ImageView) findViewById(R.id.imageview_top1);//辅助图1
		imageview_top2 = (ImageView) findViewById(R.id.imageview_top2);//辅助图2
		imageview_top3 = (ImageView) findViewById(R.id.imageview_top3);//辅助图3



		Bundle extras = getIntent().getExtras();
		String hh = extras.getString("username");//。。。获得username
		u_nick = extras.getString("nicheng");//获得从主页面传来的昵称
		String hh2 = extras.getString("sex");//。。。性别
		 String hh3 = extras.getString("zan");//。。。性别

		who = hh;
		who_zan=hh3;
		username.setText(hh.toString());
		nicheng.setText(u_nick.toString());
		sex.setText(hh2.toString());
		zan.setText(hh3.toString());
		fb.display(imageview_my,Client.Image_URL+who+".png");



		//辅助图片加载
		fuzhu1 = sjs_function();
		fuzhu2 = sjs_function();
		fuzhu3 = sjs_function();
		fb.display(imageview_top1,Client.Image_URL+"location_fuzhu"+fuzhu1+".jpg");
		while(true){//防止图片重复
			if(fuzhu1 != fuzhu2){
				break;
			}else{
				fuzhu2 = sjs_function();
			}
		}
		fb.display(imageview_top2,Client.Image_URL+"location_fuzhu"+fuzhu2+".jpg");
		while(true){//防止图片重复
			if(fuzhu1 != fuzhu2 && fuzhu1 != fuzhu3 && fuzhu2 != fuzhu3){
				break;
			}else{
				fuzhu3 = sjs_function();
			}
		}

		fb.display(imageview_top3,Client.Image_URL+"location_fuzhu"+fuzhu3+".jpg");


		/*----------------------点击赞后图片变色，赞数加1----------------------------------------*/
		imageview_zan.setOnClickListener(new OnClickListener() {

			@Override   
			public void onClick(View v) {

				int num=0;//赞加1
				try{
					num=Integer.parseInt(who_zan.toString().trim());					
					num++;			
				} catch(NumberFormatException e){
					num=-1;
				}
				zanta(who);
				zan.setText(num+"");
				Resources resources = getBaseContext().getResources();//切换背景图片   
				Drawable btnDrawable = resources.getDrawable(R.drawable.location_zan2);  
				imageview_zan.setImageDrawable(btnDrawable);

			}
		});


		/*----------------------加好友----------------------------------------*/
		add_friend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent =new Intent(Fragment_find_AddFriend.this,Friend_infoActivity.class);
				intent.putExtra("yh_unick", u_nick);//这是好友头像
				startActivity(intent);
				finish();
			}
		});

		/*----------------------给好友发消息----------------------------------------*/
//		message_friend.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//
//			}
//		});



	}

	public void zanta(final String str){
		new Thread(new Runnable() { 
			@Override
			public void run() {
				try {
					HttpClient client = new DefaultHttpClient();
					//提交数据的路径
					HttpPost httpPost =new HttpPost(URL_ZAN);
					//指定要提交数据的实体
					List<NameValuePair> parameters = new ArrayList<NameValuePair>();
					parameters.add( new BasicNameValuePair("username", URLEncoder.encode(str,"utf-8")));
					httpPost.setEntity(new UrlEncodedFormEntity(parameters,"utf-8"));
					//敲回车
					HttpResponse response = client.execute(httpPost);
					int code = response.getStatusLine().getStatusCode();
					Message msg = new Message() ;
					if (code == 200) {


						InputStream is = response.getEntity().getContent();
						String result = StreamTools.readInputStream(is);
						//System.out.println("result..<><><><><>..."+result);
						if(result!=null){
							if(result.equals("true")){							
								msg.what = 1 ;
								Fragment_find_AddFriend.this.handler.sendMessage(msg);
							}
							else{							
								msg.what = 2 ;
								Fragment_find_AddFriend.this.handler.sendMessage(msg);
							}
						}else{											
							msg.what = 2 ;
							Fragment_find_AddFriend.this.handler.sendMessage(msg);
						}
					}  else{												
						msg.what = 2 ;
						Fragment_find_AddFriend.this.handler.sendMessage(msg);
					}

				} catch (UnsupportedEncodingException e) {

					e.printStackTrace();

				} catch (ClientProtocolException e) {

					e.printStackTrace();

				} catch (IOException e) {

					e.printStackTrace();

				}

			}

		}).start();
	}




	//获得随机数
	public int  sjs_function(){
		int a1;
		a1=Integer.valueOf((int) (Math.random()*12))+1;	
		return a1;

	} 
}
